package com.example.takeoff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeoff.common.Result;
import com.example.takeoff.entity.DDL;
import com.example.takeoff.mapper.DDLMapper;
import com.example.takeoff.service.DDLService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DDLServiceImpl extends ServiceImpl<DDLMapper, DDL> implements DDLService
{
    @Autowired
    private DDLMapper ddlMapper;


    @Override
    public List<DDL> getByUserid(String userid)
    {
        LambdaQueryWrapper<DDL> reminderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<DDL> eq = reminderLambdaQueryWrapper.eq(DDL::getUserid, userid).eq(DDL::getDeleted, 0);
        List<DDL> aa = ddlMapper.selectList(eq);
        return ddlMapper.selectList(eq);
    }

    @Override
    public boolean modify(DDL ddl)
    {
        return ddlMapper.updateById(ddl) > 0;
    }


    @Override
    public IPage<DDL> getPage(int currentPage, int pageSize)
    {
        IPage page = new Page(currentPage, pageSize);
        ddlMapper.selectPage(page, null);
        return page;
    }

    @Override
    public IPage<DDL> getPage(int currentPage, int pageSize, DDL ddl)
    {
        LambdaQueryWrapper<DDL> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(ddl.getDdl()), DDL::getDdl, ddl.getDdl());
        IPage page = new Page(currentPage, pageSize);
        ddlMapper.selectPage(page, qw);
        return page;
    }

    @Override
    public List<DDL> getClassifiedDDL(String userid)
    {
        LambdaQueryWrapper<DDL> qw = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<DDL> eq = qw.eq(DDL::getUserid, userid).eq(DDL::getDeleted, 1);
        //qw.eq(DDL::getDeleted,1);

        return ddlMapper.selectList(eq);
    }

    @Override
    public Result delete(int id)
    {
        return changeState(id, 2);
    }

    @Override
    public Result classify(int id)
    {
        return changeState(id, 1);
    }

    @Override
    public Result recover(int id)
    {
        return changeState(id, 0);
    }

    private Result changeState(int id, int state)
    {

        DDL ddl = ddlMapper.selectById(id);
        if (ddl == null)
        {
            return Result.error("该DDL不存在");
        }
        ddl.setDeleted(state);
        ddlMapper.updateById(ddl);
        return Result.success("操作成功");
    }


}
